<?php

class excution extends Backend_Controller {
    /*
      Project     : 48c6c450f1a4a0cc53d9585dc0fee742
      Created on  : Mar 16, 2013, 11:29:15 PM
      Author      : Truong Khuong - khuongxuantruong@gmail.com
      Description :
      Purpose of the stylesheet follows.
     */

    function __construct() {
        parent::__construct();
    }

    function index() {
        
    }
    function convertCurrency(){
		$num = str_replace(',','.',$this->input->get_post('num'));
		$R["result"] = 1;
        $R["message"] = ("SUCCESS.");
		$R["money"] = (int)get_currency("USD","VND",$num);
		$this->output->set_header('Content-type: application/json');
        $this->output->set_output(json_encode($R));
	}
	function clear_cache(){
            if(file_exists(APPPATH.'../public/cache/') && is_dir(APPPATH.'../public/cache/'))
			$this->config->set_item('cache_path', '../public/cache/');
			$this->load->driver('cache');
			if($this->cache->file->clean()){
                echo '<script type="text/javascript">alert("Success. All caches has been clean.")</script>';
            }else{
                echo '<script type="text/javascript">alert("Fail")</script>';
            }
	}
	
    function delete($action = '', $table = '', $prefix = '') {
        $R["result"] = -1;
        $R["message"] = ("This function to requires an administrative account.<br/>Please check your authority, and try again.");
        if ($this->privilege->aupr_permission == 777) {
            $this->pmodel = new Backend_Model($table, $prefix);
            $id = (int) $this->input->post('Id');
            if ($id > 0) {
                $tmp = $this->pmodel->onGet($id);
                if ($tmp) {
                    if (
                            (isset($tmp->{$prefix . "lock"})) &&
                            $tmp->{$prefix . "lock"} == 1 &&
                            $_SESSION["auth"]["user"]->ause_authority != 'Administrator'
                    ) {
                        $R["message"] = ("Can't delete 'System' record. Record have been locked.");
                    } else {
                        $rs = $this->pmodel->onDelete($id);
                        if ($rs === true) {
                            $R["result"] = 1;
                            $R["message"] = ("Success. Record have been deleted.");
                        } else {
                            $R["result"] = -1;
                            $R["error_number"] = $this->db->_error_number();
                            $R["error_message"] = $this->db->_error_message();
                            $R["message"] = ("Fail. Please check data input and try again.<br/>");
                            if ($R["error_number"] != 0)
                                $R["message"].=("<span class='erc'>[" . $R["error_number"] . "] " . $R["error_message"] . ".</span>");
                        }
                    }
                }else {
                    $R["message"] = ("Record does't exist.");
                }
            } else {
                $R["message"] = ("Id invalid.");
            }
        }
        $this->output->set_header('Content-type: application/json');
        $this->output->set_output(json_encode($R));
    }
    
    function databinding($action = '', $table = '', $prefix = ''){
        $this->pmodel=new Backend_Model($table);
        $result = $this->pmodel->databinding();
        $this->output->set_header('Content-type: application/json');
        $this->output->set_output(json_encode($result));
    }
    
    function commit($action = '', $table = '', $prefix = '') {
        $R["result"] = -1;
        $R["message"] = ascii_to_entities("This function to requires an administrative account.<br/>Please check your authority, and try again.");
        if ($this->privilege->aupr_permission == 777) {
            
            $this->pmodel = new Backend_Model($table, $prefix);
            $Params = $this->input->get_post('Params');
            //fix content
            if(isset($Params['Content']))$Params['Content'] = $_REQUEST['Params']['Content'];
            $Id = (int) $this->input->post('Id');
            if ($Id != 0) {
                $record=$this->pmodel->onGet($Id);
                if($record){
                    $rs = $this->pmodel->onUpdate($Id, $Params);
                }else{
                    $R["result"] = -203;
                    $R["message"] = "Record doesn't exist.";
                    $this->output->set_header('Content-type: application/json');
                    $this->output->set_output(json_encode($R));
                    return;
                }
            } else {
                $rs = $this->pmodel->onInsert($Params);
            }
            if ($rs === true) {
                $R["result"] = 1;
                $R["message"] = ascii_to_entities("Success. Data have been save.");
            } else {
                $R["result"] = -1;
                $R["error_number"] = $this->db->_error_number();
                $R["error_message"] = $this->db->_error_message();
                //$R["last_query"] = $this->db->last_query();
                $R["message"] = ascii_to_entities("System rejected your data.<br/>Please check data input and try again.<br/>");
                if ($R["error_number"] != 0){
                    $R["message"].=ascii_to_entities("<span class='erc'>[" . $R["error_number"] . "] " . $R["error_message"] . ".</span>");
                }
                $R['p']=$Params;
            }
        }
        $this->output->set_header('Content-type: application/json');
        $this->output->set_output(json_encode($R));
    }

}

?>
